package com.wang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.entity.TbBbsArticle;
import com.wang.entity.TbBbsCollect;
import com.wang.mapper.TbBbsArticleMapper;
import com.wang.mapper.TbBbsCollectMapper;
import com.wang.response.ResultResponse;
import com.wang.service.TbBbsCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *收藏
 */
@Service
public class TbBbsCollectServiceImpl implements TbBbsCollectService{
    @Autowired
    private TbBbsCollectMapper tbBbsCollectMapper;
    @Autowired
    private TbBbsArticleMapper tbBbsArticleMapper;

    /*
    添加收藏
     */
    @Override
    public ResultResponse insertcollect(TbBbsCollect tbBbsCollect) {
        int insert = tbBbsCollectMapper.insert(tbBbsCollect);
        if (insert>0){
            return ResultResponse.SUCCESS();
        }else{
            return ResultResponse.FAIED(500, "添加失败");
        }
    }


    /*
    删除收藏
     */
    @Override
    public ResultResponse removecollect(Integer collectId) {
        int i = tbBbsCollectMapper.deleteById(collectId);
        if (i>0){
            return ResultResponse.SUCCESS();
        }else{
            return ResultResponse.FAIED(500, "删除收藏失败");
        }
    }

    /*
    查询收藏
     */
    @Override
    public ResultResponse findcollect(TbBbsCollect tbBbsCollect) {
        QueryWrapper<TbBbsCollect> wrapper = new QueryWrapper<>();
        ArrayList list = new ArrayList();
        wrapper.eq("user_id", tbBbsCollect.getUserId());
        List<TbBbsCollect> bbsCollects = tbBbsCollectMapper.selectList(wrapper);
        for (TbBbsCollect collect : bbsCollects) {
            Integer fid = collect.getFileId();
            QueryWrapper<TbBbsArticle> articleQueryWrapper = new QueryWrapper<>();
            articleQueryWrapper.eq("file_id", fid);
            List<TbBbsArticle> bbsArticles = tbBbsArticleMapper.selectList(articleQueryWrapper);
            list.add(bbsArticles);
        }
        return ResultResponse.SUCCESS(list);
    }

    /*
    查询收藏所有
     */
    @Override
    public ResultResponse findcollectall(TbBbsCollect tbBbsCollect) {
        ArrayList list = new ArrayList();
        Page<TbBbsCollect> page = new Page<>(tbBbsCollect.getPagenum(), tbBbsCollect.getPagesize());
        QueryWrapper<TbBbsCollect> wrapper = new QueryWrapper<>();
        Page<TbBbsCollect> page1 = tbBbsCollectMapper.selectPage(page, wrapper);
        long total = page1.getTotal();
        return ResultResponse.SUCCESS(page1.getRecords(),total);
    }
}




